<?php
namespace App\Http\Controllers\Alipay;

use App\Models\Suggest;
use App\Models\SuggestLog;
use App\Models\SuggestType;
use App\Services\CommonService;
use Illuminate\Http\Request;

//投诉建议
class ComplaintSuggestController extends BaseController
{
    //获取投诉类型 http://shop.cn/Alipay/ComplaintSuggest/getSuggestType
    public function getSuggestType()
    {
        $suggestType = SuggestType::GetSuggestType();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $suggestType);
    }

    //提交投诉建议POST http://shop.cn/Alipay/ComplaintSuggest/submitComplaintSuggest
    //{"suggest_type_id":"","suggest_cate":"","suggest_title":"","content":"","images":""}
    public function submitComplaintSuggest(Request $request)
    {
        $input = $request->input();
        if (empty($input['suggest_type_id']) || empty($input['suggest_cate']) || empty($input['suggest_title']) || empty($input['content']) || empty($input['images'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $member = $this->member; //用户信息
        $suggest_count = Suggest::where('user_id',$member['id'])->whereBetween('create_time', [date('Y-m-d 00:00:00'), date('Y-m-d 23:59:59')])->count();
        if ($suggest_count >= 2) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '一天只能提交2次投诉建议');
        }
        $suggestType = SuggestType::GetOneSuggestType(array('id' => $input['suggest_type_id']));
        $type_time = $suggestType['type_time'];
        $addData = array(
            'user_id' => $member['id'], //用户id(关联sp_member_2中的id)
            'user_mobile' => $member['mobile'], //用户手机号
            'suggest_type' => $suggestType['type_name'], //投诉类型
            'suggest_cate' => $input['suggest_cate'], //投诉类别：1商品名称  2商家名称  3订单编号
            'suggest_title' => $input['suggest_title'], //类别内容：投诉商品名称/商家名称/订单编号
            'content' => $input['content'], //投诉详情
            'images' => $input['images'], //图片
            'grade' => $suggestType['type_grade'], //工单等级：1普通 2紧急 3严重
            'end_time' => date('Y-m-d H:i:s', strtotime("+$type_time hours", time())), //截止时间
        );
        $id = Suggest::addData($addData);
        if (!$id) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '提交失败');
        } else {
            $res = SuggestLog::addData($id, $member['id'], '', '', SuggestLog::SUGGEST_LOG_HANDLE_TYPE_APPLY_COMPLAINT);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '提交成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '提交失败');
            }
        }
    }

    //根据类型获取投诉建议POST http://shop.cn/Alipay/ComplaintSuggest/getSuggest  {"type":""}
    public function getSuggest(Request $request)
    {
        $input = $request->input();
        if (empty($input['type'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $member = $this->member; //用户信息
        if ($input['type'] == 1) {
            $status = array(
                Suggest::SUGGEST_STATUS_NO_HANDLE,
                Suggest::SUGGEST_STATUS_HANDLING
            );
        } elseif ($input['type'] == 2) {
            $status = array(
                Suggest::SUGGEST_STATUS_HANDLED
            );
        }
        $suggest = Suggest::select('id', 'work_order', 'suggest_type', 'suggest_cate', 'suggest_title', 'content', 'images', 'status')
            ->whereIn('status', $status)
            ->where('user_id', $member['id'])
            ->get();
        $suggest = $suggest ? $suggest->toArray() : [];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $suggest);
    }

    //撤销POST http://shop.cn/Alipay/ComplaintSuggest/cancel  {"suggest_id":""}
    public function cancel(Request $request)
    {
        $input = $request->input();
        if (empty($input['suggest_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $member = $this->member; //用户信息
        $suggest = Suggest::GetOneSuggest(array('id' => $input['suggest_id'], 'user_id' => $member['id']));
        if ($suggest['status'] != Suggest::SUGGEST_STATUS_NO_HANDLE && $suggest['status'] != Suggest::SUGGEST_STATUS_HANDLING) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该工单状态不是待处理或者处理中');
        }
        $edit_res = Suggest::where(array('id' => $input['suggest_id'], 'user_id' => $member['id']))->update(array('status' => Suggest::SUGGEST_STATUS_CANCEL, 'update_time' => date('Y-m-d H:i:s')));
        if (!$edit_res) {
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '撤销失败');
        } else {
            $res = SuggestLog::addData($input['suggest_id'], $member['id'], '', '', SuggestLog::SUGGEST_LOG_HANDLE_TYPE_CANCEL_COMPLAINT);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '撤销成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '撤销失败');
            }
        }
    }

    //未解决POST http://shop.cn/Alipay/ComplaintSuggest/unresolved  {"suggest_id":"","content":""}
    public function unresolved(Request $request)
    {
        $input = $request->input();
        if (empty($input['suggest_id']) || empty($input['content'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $member = $this->member; //用户信息
        $suggest = Suggest::GetOneSuggest(array('id' => $input['suggest_id'], 'user_id' => $member['id']));
        if ($suggest['status'] != Suggest::SUGGEST_STATUS_HANDLING) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该工单状态不是处理中');
        }
        $res = SuggestLog::addData($input['suggest_id'], $member['id'], '', $input['content'], SuggestLog::SUGGEST_LOG_HANDLE_TYPE_UNRESOLVED_COMPLAINT);
        if ($res) {
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '操作成功');
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '操作失败');
        }
    }

    //已解决POST http://shop.cn/Alipay/ComplaintSuggest/resolved  {"suggest_id":""}
    public function resolved(Request $request)
    {
        $input = $request->input();
        if (empty($input['suggest_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $member = $this->member; //用户信息
        $suggest = Suggest::GetOneSuggest(array('id' => $input['suggest_id'], 'user_id' => $member['id']));
        if ($suggest['status'] != Suggest::SUGGEST_STATUS_HANDLING) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该工单状态不是处理中');
        }
        $edit_res = Suggest::where(array('id' => $input['suggest_id'], 'user_id' => $member['id']))->update(array('status' => Suggest::SUGGEST_STATUS_HANDLED, 'update_time' => date('Y-m-d H:i:s')));
        if (!$edit_res) {
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '操作失败');
        } else {
            $res = SuggestLog::addData($input['suggest_id'], $member['id'], '', '', SuggestLog::SUGGEST_LOG_HANDLE_TYPE_RESOLVED_COMPLAINT);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '操作成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '操作失败');
            }
        }
    }

    //查看进度 http://shop.cn/Alipay/ComplaintSuggest/lookProgress?suggest_id=
    public function lookProgress(Request $request)
    {
        $input = $request->input();
        if (empty($input['suggest_id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $member = $this->member; //用户信息
        $suggest = Suggest::GetOneSuggest(array('id' => $input['suggest_id'], 'user_id' => $member['id']));
        $suggestLog = SuggestLog::GetSuggestLog(array('suggest_id' => $input['suggest_id']), array(SuggestLog::SUGGEST_LOG_HANDLE_TYPE_REMARKS));
        foreach ($suggestLog as $k => $v) {
            if ($v['handle_type'] == SuggestLog::SUGGEST_LOG_HANDLE_TYPE_APPLY_COMPLAINT) {
                $suggestLog[$k]['suggest_type'] = $suggest['suggest_type'];
                $suggestLog[$k]['suggest_cate'] = $suggest['suggest_cate'];
                $suggestLog[$k]['suggest_title'] = $suggest['suggest_title'];
                $suggestLog[$k]['suggest_content'] = $suggest['content'];
                $suggestLog[$k]['images'] = $suggest['images'];
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $suggestLog);
    }


}
